package com.hong.qrpc.utils;

import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author HQT
 * @Date 2022/7/16 15:41
 */
public class StringUtils {
    public static boolean isEmpty(String s) {
        return s == null || "".equals(s) || "null".equalsIgnoreCase(s);
    }

    public static String[] splitByChars(String s, char... chars) {
        HashSet<Character> charSet = new HashSet<>();
        for (char ban : chars) {
            charSet.add(ban);
        }
        Queue<String> temp = new LinkedList<>();
        for (int i = 0, limit = s.length(); i < limit; ) {
            StringBuilder sb = new StringBuilder();
            char cur = s.charAt(i++);
            while (i < limit && !charSet.contains(cur)) {
                sb.append(cur);
                cur = s.charAt(i++);
            }
            if (!charSet.contains(cur)) {
                sb.append(cur);
            }
            temp.offer(sb.toString());
        }
        String[] res = new String[temp.size()];
        for (int i = 0; !temp.isEmpty(); i++) {
            res[i] = temp.poll();
        }
        return res;
    }

    public static String getClassName(Deque<String> subUrls) {
        StringBuilder sb = new StringBuilder(subUrls.size() << 3);
        for (String subUrl : subUrls) {
            sb.append(subUrl).append(".");
        }
        return sb.substring(0, sb.length() - ".class.".length());
    }

    public static String replaceAll(char oldChar, char newChar, String str) {
        StringBuilder sb = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            char cur = str.charAt(i);
            if (cur == oldChar) {
                sb.append(newChar);
            } else {
                sb.append(cur);
            }
        }
        return sb.toString();
    }

    public static String getSimpleName(Class clazz) {
        String srcName = clazz.getName();
        String simpleName = srcName.substring(srcName.lastIndexOf('.') + 1);
        return simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
    }
}
